<template>
  <div class="register-container">
    <div class="register-background">
      <div class="background-overlay"></div>
    </div>

    <div class="register-card">
      <div class="register-header">
        <div class="logo">
          <el-icon size="40" color="#52c41a"><UserFilled /></el-icon>
        </div>
        <h2 class="title">用户注册</h2>
        <p class="subtitle">创建您的农产品销售平台账户</p>
      </div>

      <el-form
        ref="registerFormRef"
        :model="registerForm"
        :rules="registerRules"
        class="register-form"
        label-width="80px"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            v-model="registerForm.username"
            placeholder="请输入账号"
            size="large"
            :prefix-icon="User"
            clearable
          />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            :prefix-icon="Lock"
            show-password
            clearable
          />
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            size="large"
            :prefix-icon="Lock"
            show-password
            clearable
          />
        </el-form-item>

        <el-form-item v-if="tableName === 'shangjia'" label="商家名称" prop="shangjiaName">
          <el-input
            v-model="registerForm.shangjiaName"
            placeholder="请输入商家名称"
            size="large"
            :prefix-icon="Shop"
            clearable
          />
        </el-form-item>

        <el-form-item v-if="tableName === 'shangjia'" label="联系方式" prop="shangjiaPhone">
          <el-input
            v-model="registerForm.shangjiaPhone"
            placeholder="请输入联系方式"
            size="large"
            :prefix-icon="Phone"
            clearable
          />
        </el-form-item>

        <el-form-item v-if="tableName === 'shangjia'" label="邮箱" prop="shangjiaEmail">
          <el-input
            v-model="registerForm.shangjiaEmail"
            placeholder="请输入邮箱地址"
            size="large"
            :prefix-icon="Message"
            clearable
          />
        </el-form-item>

        <el-button
          type="primary"
          size="large"
          :loading="loading"
          class="register-button"
          @click="handleRegister"
        >
          {{ loading ? '注册中...' : '立即注册' }}
        </el-button>

        <div class="register-footer">
          <el-link type="primary" @click="goToLogin">已有账号？立即登录</el-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, UserFilled, Shop, Phone, Message } from '@element-plus/icons-vue'

const router = useRouter()

// 响应式数据
const loading = ref(false)
const registerFormRef = ref(null)
const tableName = ref('shangjia') // 默认商家注册

const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  shangjiaName: '',
  shangjiaPhone: '',
  shangjiaEmail: ''
})

// 表单验证规则
const registerRules = {
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 20, message: '账号长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  shangjiaName: [
    { required: true, message: '请输入商家名称', trigger: 'blur' }
  ],
  shangjiaPhone: [
    { required: true, message: '请输入联系方式', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  shangjiaEmail: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ]
}

// 方法
const handleRegister = async () => {
  if (!registerFormRef.value) return

  try {
    await registerFormRef.value.validate()
    loading.value = true

    const registerData = {
      username: registerForm.username,
      password: registerForm.password, // 暂时不使用MD5加密
      shangjiaName: registerForm.shangjiaName,
      shangjiaPhone: registerForm.shangjiaPhone,
      shangjiaEmail: registerForm.shangjiaEmail
    }

    // 模拟API调用
    console.log('注册数据:', registerData)
    
    // 这里应该调用实际的API
    // const result = await $http({
    //   url: `${tableName.value}/register`,
    //   method: 'post',
    //   data: registerData
    // })

    // 模拟成功响应
    ElMessage.success('注册成功，请登录')
    router.push('/login')
    
  } catch (error) {
    console.error('注册错误:', error)
    ElMessage.error('注册失败，请检查表单填写')
  } finally {
    loading.value = false
  }
}

const goToLogin = () => {
  router.push('/login')
}

// 生命周期
onMounted(() => {
  // 从localStorage获取注册类型
  const loginTable = localStorage.getItem('loginTable')
  if (loginTable) {
    tableName.value = loginTable
  }
})
</script>

<style lang="scss" scoped>
@import '@/styles/variables.scss';

.register-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.register-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.background-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.2);
}

.register-card {
  position: relative;
  z-index: 10;
  width: 500px;
  max-height: 90vh;
  padding: 40px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  overflow-y: auto;
}

.register-header {
  text-align: center;
  margin-bottom: 32px;

  .logo {
    margin-bottom: 16px;
  }

  .title {
    font-size: 24px;
    font-weight: 600;
    color: $text-color-primary;
    margin-bottom: 8px;
  }

  .subtitle {
    color: $text-color-secondary;
    font-size: 14px;
  }
}

.register-form {
  .el-form-item {
    margin-bottom: 20px;
  }

  .register-button {
    width: 100%;
    height: 48px;
    font-size: 16px;
    font-weight: 500;
    background: linear-gradient(135deg, $agriculture-green, $agriculture-light-green);
    border: none;
    border-radius: 8px;
    margin-bottom: 16px;

    &:hover {
      background: linear-gradient(135deg, $agriculture-light-green, $agriculture-green);
      transform: translateY(-1px);
      box-shadow: 0 4px 12px rgba(82, 196, 26, 0.3);
    }
  }
}

.register-footer {
  text-align: center;

  .el-link {
    font-size: 14px;
  }
}

// 响应式设计
@media (max-width: 600px) {
  .register-card {
    width: 90%;
    padding: 24px;
  }
}

// 动画效果
.register-card {
  animation: slideUp 0.6s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
